package com.itheima.类加载与反射.反射.案例五;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;

public class 反射属性 {
    public static void main(String[] args) throws Exception {
        //demo1();
        //ArrayList list = (ArrayList) cl.newInstance();
        Class cl = Class.forName("java.util.ArrayList");
        Field size = cl.getDeclaredField("size");
        size.setAccessible(true);
        ArrayList list = (ArrayList) cl.getDeclaredConstructor().newInstance();
        System.out.println(size.get(list));
        size.set(list, 10);
        System.out.println(size.get(list));
        System.out.println("==========================");
        list.add("abc");
        System.out.println(list);

    }

    public static void demo1() throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Class cl = Class.forName("java.util.HashMap");
        Field field = cl.getDeclaredField("DEFAULT_LOAD_FACTOR");
        Constructor con = cl.getDeclaredConstructor();
        HashMap hap = (HashMap) con.newInstance();
        //System.out.println(HashMap.DEFAULT_LOAD_FACTOR);
        field.setAccessible(true);
        System.out.println(field.get(hap));
    }
}
